package com.zac.coroutine.algorithm.leetcode.strings

/**
 * author zac
 * date 2025/8/5
 *
 */
object WordPattern {

    @JvmStatic
    fun main(args: Array<String>) {
        //  pattern = "abba", s = "dog cat cat dog"
        // "dog dog dog dog"
        println(wordPattern("abba", "dog cat cat fish"))
    }

    fun wordPattern(pattern: String, s: String): Boolean {
        val a = s.split(" ")

        if (pattern.length != a.size) {
            return false
        }
        val map = HashMap<Char, String>()
        val map1 = HashMap<String, Char>()
        for (i in 0 until pattern.length) {
            if (map.contains(pattern[i])) {
                if (map[pattern[i]] != a[i]) {
                    return false
                }
            } else {
                map.put(pattern[i], a[i])
            }
            if (map1.contains(a[i])) {
                if (map1[a[i]] != pattern[i]) {
                    return false
                }
            } else {
                map1.put(a[i], pattern[i])
            }
        }

        return true
    }


}